package com.yanzhenjie.album.sample;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;


import com.nightonke.boommenu.BoomButtons.BoomButton;
import com.nightonke.boommenu.BoomMenuButton;
import com.nightonke.boommenu.OnBoomListener;
import com.yanzhenjie.album.Action;
import com.yanzhenjie.album.Album;
import com.yanzhenjie.album.api.widget.Widget;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;
import pub.devrel.easypermissions.PermissionRequest;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
public class FullscreenActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks  {

    private String[] classRes =new String[]{"robot_class", "program_class"};
    private String[] gradeRes =new String[]{"grade_child_1", "grade_child_2","grade_child_3","grade_1","grade_2","grade_3","grade_4","grade_5","grade_6",
    };
    File file = Environment.getExternalStorageDirectory();
    String rootClassPath = null;
    String rootGradePath = null;
    String studentInfoPath = null;
    String teachInfoPath = null;
    String schoolInfoPath = null;
    String awardInfoPath = null;
    ArrayList<String> imageList = new ArrayList<>();
    private BoomMenuButton bmb2;
    private BoomMenuButton bmb3;
    private LinearLayout ll1,ll2,ll3,ll4,ll5;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fullscreen);
        requestPermission();
        rootClassPath = file.getAbsolutePath()+"/crops/class/";
        rootGradePath = file.getAbsolutePath()+"/crops/grade/";
        studentInfoPath = file.getAbsolutePath()+"/crops/student/";
        teachInfoPath = file.getAbsolutePath()+"/crops/teach/";
        schoolInfoPath = file.getAbsolutePath()+"/crops/school/";
        awardInfoPath = file.getAbsolutePath()+"/crops/award/";
        bmb2 = (BoomMenuButton) findViewById(R.id.bmb2);
        ll2 = (LinearLayout)  findViewById(R.id.layout_ll2);
        ll3 = (LinearLayout)  findViewById(R.id.layout_ll3);
        ll4 = (LinearLayout)  findViewById(R.id.layout_ll4);
        ll5 = (LinearLayout)  findViewById(R.id.layout_ll5);
        ll2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoImages(teachInfoPath);
            }
        });
        ll3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoImages(studentInfoPath);
            }
        });
        ll4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoImages(awardInfoPath);
            }
        });
        ll5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoImages(schoolInfoPath);
            }
        });

        bmb2.setBuilders( BuilderManager.getClassType());
        bmb2.setOnBoomListener(new OnBoomListener() {
            @Override
            public void onClicked(int index, BoomButton boomButton) {
                String name  = classRes[index];
                imageList.clear();
                imageList.add(rootClassPath+name+".jpg");
                previewImages();
                Log.d("BoomMenuButton  ","click "+index + " - "+boomButton.getTextView().getText().toString());
            }

            @Override
            public void onBackgroundClick() {

            }

            @Override
            public void onBoomWillHide() {

            }

            @Override
            public void onBoomDidHide() {

            }

            @Override
            public void onBoomWillShow() {

            }

            @Override
            public void onBoomDidShow() {

            }
        });
        bmb3 = (BoomMenuButton) findViewById(R.id.bmb3);
        bmb3.setBuilders( BuilderManager.getGradeType());
        bmb3.setOnBoomListener(new OnBoomListener() {
            @Override
            public void onClicked(int index, BoomButton boomButton) {
                String name  = gradeRes[index];
                imageList.clear();
                imageList.add(rootGradePath+name+".jpg");
                previewImages();
                Log.d("BoomMenuButton  ","click "+index + " - "+boomButton.getTextView().getText().toString());
            }

            @Override
            public void onBackgroundClick() {

            }

            @Override
            public void onBoomWillHide() {

            }

            @Override
            public void onBoomDidHide() {

            }

            @Override
            public void onBoomWillShow() {

            }

            @Override
            public void onBoomDidShow() {

            }
        });

        bmb2.setUse3DTransformAnimation(true);
        bmb3.setUse3DTransformAnimation(true);
        bmb2.setDuration(1000);
        bmb3.setDuration(1000);

    }


    private void previewImages() {
        Log.d("previewImages","imageList = "+ imageList.toString());
        Album.gallery(this)
                .checkedList(imageList)
                .checkable(false)
                .widget(
                        Widget.newDarkBuilder(this)
                                .title("image")
                                .build()
                )
                .onResult(new Action<ArrayList<String>>() {
                    @Override
                    public void onAction(@NonNull ArrayList<String> result) {
                        // TODO If it is optional, here you can accept the results of user selection.
                    }
                })
                .start();
    }

    private void gotoImages(String folderPath){
        ArrayList<String> localFiles=new ArrayList<>();
        File folderFile = new File(folderPath);
        if(folderFile!=null) {
            for (File image : folderFile.listFiles()
            ) {
                localFiles.add(image.getAbsolutePath());
                Log.d("image", image.getAbsolutePath());
            }
        }

        Album.gallery(this)
                .checkedList(localFiles)
                .checkable(false)
                .widget(
                        Widget.newDarkBuilder(this)
                                .title("image")
                                .build()
                )
                .onResult(new Action<ArrayList<String>>() {
                    @Override
                    public void onAction(@NonNull ArrayList<String> result) {
                        // TODO If it is optional, here you can accept the results of user selection.
                    }
                })
                .start();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable  Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) {
//            //用户从应用设置屏幕返回后，执行一些操作，例如显示Toast.
//            Toast.makeText(this, R.string.returned_from_app_settings_to_activity, Toast.LENGTH_SHORT)
//                    .show();
        }
    }

   private void  requestPermission() {
       String[] perms = new String[0];
       if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
           perms = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
       }
       if (EasyPermissions.hasPermissions(this, perms)) {
           // 已经有权限，进行相关操作
           // ...
       } else {
           // 没有权限，进行权限请求
           EasyPermissions.requestPermissions(
                   this, "需要申请权限", 110, perms);
       }
   }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull  String[] permissions, @NonNull  int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        // 将结果转发给 EasyPermissions
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull  List<String> perms) {

    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull  List<String> perms) {
        Log.d("FullscreenActivity", "onPermissionsDenied:" + requestCode + ":" + perms.size());

        // 可选）检查用户是否拒绝了任何权限并选中了“永不再次询问”。
        //这将显示一个对话框，指导他们启用应用程序设置中的权限。
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
            new AppSettingsDialog.Builder(this).build().show();
        }
    }
}